var account = {};

window.onload = function() {
	init();
	var x = getelid('signupForm');
	if (isSet(x)) {
		if (x.login.disabled) {
			x.onoma.focus();
		}
		else {
			x.login.focus();
		}
	}
};

account.addPektis = function(form) {
	var errfld = null;
	if (form.kodikos2.value !== form.kodikos1.value) {
		fyiError('Different passwords');
		errfld = form.kodikos1;
		errfld.style.color = globals.color.error;
		errfld.select();
	}
	if (form.kodikos1.value === '') {
		fyiError('You must provide a password');
		errfld = form.kodikos1;
		errfld.style.color = globals.color.error;
		errfld.select();
	}
	if (!account.checkEmailValue(form.email)) {
		errfld = form.email;
		errfld.focus();
	}
	if (!account.checkLoginValue(form.login)) {
		errfld = form.login;
		errfld.focus();
	}
	if (isSet(errfld)) {
		return false;
	}

	var req = new Request('account/addPektis', false);
	params = 'login=' + uri(form.login.value) +
		'&onoma=' + uri(form.onoma.value) +
		'&email=' + uri(form.email.value) +
		'&password=' + uri(form.kodikos1.value);
	req.send(params);
	var rsp = req.getResponse();
	if (rsp) {
		fyiError(rsp);
		return false;
	}

	window.location.href = globals.server + 'index.php';
	return false;
};

account.isodos = function(form) {
	var req = new Request('account/isodos', false);
	params = 'login=' + uri(form.login.value.trim()) +
		'&password=' + uri(form.kodikos.value);
	req.send(params);
	var rsp = req.getResponse();
	if (rsp) {
		fyiError(rsp);
		return false;
	}

	window.location.href = globals.server + 'index.php';
	return false;
};

account.checkLoginValue = function(fld) {
	if (isSet(fld)) { fld.value = fld.value.trim(); }
	else { fld.value = ''; }

	if (fld.value === '') {
		fld.style.color = globals.color.error;
		fyiError('You must provide a valid login name');
		return false;
	}

	if (!fld.value.match(/^[a-zA-Z][a-zA-Z0-9_!@#=.:+-]*$/)) {
		fld.style.color = globals.color.error;
		fyiError('Invalid login name');
		return false;
	}

	fld.style.color = globals.color.ok;
	return true;
};

account.checkEmailValue = function(fld) {
	if (isSet(fld)) {
		fld.value = fld.value.trim();
	}
	else {
		fld.value = '';
	}

	if ((fld.value === '') || validEmail(fld.value)) {
		fld.style.color = globals.color.ok;
		return true;
	}

	fyiError('Invalid email');
	fld.style.color = globals.color.error;
	return false;
};
